<?php

use Biz\model\brandActivity\BrandShowActivity;
use Biz\module\Classes\wiki\Infos;
use Biz\service\brand\Brand;
use Doraemon\service\Article;
use Doraemon\service\Blank;
use Doraemon\service\Guide;
use Doraemon\service\Photos;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\BlockType;

/**
 * @deprecated
 * Description: 活动品牌详情页
 * Created by PhpStorm.
 * User: congyukun
 * Date: 2021/3/11
 * Time: 16:44
 */
class BrandDetailsAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    
    public function main()
    {
        $brand_id = $this->getRequest()->getPost('brand_id', 0);
        $uid = $this->uid;
        if (empty($brand_id)) {
            throw new \Exception('参数错误');
        }
        
        $a_where['brand_id'] = $brand_id;
        $info = BrandShowActivity::getInfoByWhere($a_where);
        if (is_null($info)) {
            throw new \Exception('非法传参');
        }
        //获取 wiki_ids
        $wiki_ids = $info['wiki_ids'] ? explode(',', $info['wiki_ids']) : [];
        
        $brand_info = Brand::getBrandInfoByIds4Goods([$brand_id]);
        //品牌信息
        $data['brand_info'] = $this->_formatBrandInfos($brand_info[$brand_id], $info);
        $wiki_list = [];
        if ($wiki_ids) {
            //商品信息
            $wiki_list = Infos::infos($wiki_ids, Infos::FIELDS_GROUP_BASE_LIST, $uid);
            $wiki_list = $wiki_list ? array_values($wiki_list) : [];
            
        }
        
        $data['wiki_list'] = $wiki_list;
        
        //配置的内容
        $obj_ids = $info['obj_ids'] ? explode(',', $info['obj_ids']) : [];
        $content_list = $this->_getContentByIds($obj_ids, $uid);
        $data['content_list'] = array_values($content_list);
    
    
        $lex = [
            'http://img.hhz1.cn/Op-imageShow/71ff1218y0xq0aM00qq7pzg?iv=1',
            'http://img.hhz1.cn/Op-imageShow/ff76c20ro15h0CH00qq7q09?iv=1',
            'http://img.hhz1.cn/Op-imageShow/2d24520u00i00jO00qq7q0o?iv=1',
            'http://img.hhz1.cn/Op-imageShow/3f8d330cu0j00NO00qq7q9t?iv=1'
        ];
        //莱克更改图片
        if ($brand_id == 2453) {
            foreach ($data['content_list'] as $k => $v) {
                $data['content_list'][$k]['blank_info']['cover_pic_url'] = $lex[$k];
            }
        }
        
        $where['uid'] = $uid;
        $where['brand_id'] = $brand_id;
        //是否投票
        $is_have = \Biz\model\brandActivity\BrandJoinRecord::is_have($where);
        $data['is_voted'] = 0;
        if ($is_have) {
            $data['is_voted'] = 1;
        }
        $act_from = 'cloud_show_brand_detail';
        $extend = [];
        $data['statSign'] = basicStatSign::genStatSign(basicStatSign::MODULE_CLOUD_SHOW_BRAND_DETAIL, $extend,
            ['brand_id' => $brand_id, 'uid' => $uid, 'act_from' => $act_from]);
        $data['coupon_link'] = $info['coupon_link'] ?? '';
        $data['link_type'] = $info['link_type'] ?? 0;
        $this->setSuccess($data);
    }
    
    /**
     * 格式化 品牌信息
     * @param $brand_info
     * @param $info
     * @return array
     */
    
    public function _formatBrandInfos($brand_info, $info): array
    {
        
        $data['uid'] = $brand_info['uid'];
        $data['id'] = $brand_info['id'];
        $data['brand_type'] = $brand_info['brand_type'];
        $data['name'] = empty($info['brand_name']) ? $brand_info['name'] : $info['brand_name'];
        $data['logo'] = empty($info['logo']) ? $brand_info['logo'] : $info['logo'];
        $data['discription'] = empty($info['desc_info']) ? $brand_info['discription'] : $info['desc_info'];
        $data['address'] = $info['address'] ?? '';
        $data['coupon'] = $info['coupon'] ?? 0;
        $data['coupon_link'] = $info['coupon_link'] ?? '';
        return $data;
    }
    
    /**
     * 根据 内容id获取
     * @param $obj_ids
     * @param $uid
     * @return array
     */
    private function _getContentByIds($obj_ids, $uid): array
    {
        
        $photo_ids = [];
        $guide_ids = [];
        $blank_ids = [];
        $article_ids = [];
        foreach ($obj_ids as $obj_id) {
            $obj_type = \Doraemon\tools\IdParse::getType($obj_id);
            switch ($obj_type) {
                case BlockType::TYPE_PHOTO:
                    $photo_ids[] = $obj_id;
                    break;
                case BlockType::TYPE_GUIDE:
                    $guide_ids[] = $obj_id;
                    break;
                case BlockType::TYPE_BLANK:
                    $blank_ids[] = $obj_id;
                    break;
                case BlockType::TYPE_ARTICLE:
                    $article_ids[] = $obj_id;
                    break;
                default:
                    break;
            }
        }
        
        $article_Lists = Article::getArticlesForDynamic($article_ids, $uid);
        $photo_lists = Photos::getPhotosForDynamic($photo_ids, $uid);
        $guide_lists = Guide::getGuidesForRecommend3_2($guide_ids, $uid, ['wiki' => true]);
        $blank_lists = Blank::getBlanksForDynamic($blank_ids, $uid, ['wiki' => true]);
        
        $content_list = array_merge($article_Lists, $photo_lists, $guide_lists, $blank_lists);
        
        foreach ($content_list as $key => &$item) {
            $obj_type = \Doraemon\tools\IdParse::getType($key);
            $item['type'] = $obj_type;
        }
        return $content_list;
    }
}

